﻿package sedie.exchangeddata 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import game.events.EventData;
	import game.constants.ConstData;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class HistoryInfo extends EventDispatcher
	{
		public var history:Array;
		public var odlResult:String;// check call showresult that is loops 
		public function HistoryInfo() 
		{
			history = new Array();
		}
		
		public function setHistory(result:Array):void {
			history = result;
			dispatchEvent(new EventData(EventData.UPDATE_HISTORY));
			
		}
		
		public function returnSetHistory(result:String):Array {
			
			odlResult = result;
			var resultArr:Array = new Array();
			result = result.substr(38, result.length);
			result = result.substr(0, result.length - 1);
			
			resultArr = result.split(";");
			history = resultArr;
			businessHis(resultArr);
			
			return history;
		}
		/**
		 * @author	Bamboo
		 * @param	resultArr
		 * @usage 	prosecute data to draw history. Check the first is even or odd, then check the next history. If col is over max, function recall 
		 */
		private function businessHis(resultArr:Array):void{
	
			var colInd:uint = 0;
			var rowInd:uint = 0;
			var pos:uint = 0;
			var colNum:uint = 13;
			var rowNum:uint = 6;
			var flagEven:Boolean;
			var flagPos:Boolean = true;
			var flagTemp:Boolean;
			var flagCheckEvenOdd:Boolean;
			var tempHis:Array = new Array();
			tempHis = resultArr[0].split("-");
			if(tempHis[1]%2 == 0){
				flagEven =true;
			}
			else{
				flagEven = false;
			}
			
			for(var i:int = 1; i< resultArr.length;i++)
			{
				tempHis = new Array();
				tempHis = resultArr[i].split("-");
				flagCheckEvenOdd = checkEvenOdd(tempHis[1]);
				if (flagEven) {
					if (flagCheckEvenOdd) {
						rowInd++;
						flagTemp = true;
					}
					else {
						colInd++;
						rowInd = 0;
						flagTemp = false;
					}
				}else {
					if (flagCheckEvenOdd) {
						colInd++;
						rowInd = 0;
						flagTemp = true;
					}
					else {
						rowInd++;
						flagTemp = false;
					}
				}
				
				if(rowInd==rowNum){
					colInd++;
					rowInd=0;
				}
				
				if(colInd==1 && flagPos){
					pos = i;
					flagPos = false;
				}
				
				flagEven = flagTemp;
				if(colInd==colNum){	
					resultArr = resultArr.slice(pos, resultArr.length);
					colInd=0;
					rowInd=0;
					pos = 0;
					flagPos = true;
					
					history = resultArr;
					businessHis(resultArr);
					break;
				}
			}
			
		}
		
		private function checkEvenOdd(value:uint):Boolean {
			if(value % 2 == 0)
				return true;
			return false;
		}
	}

}